package com.gr.staffpm.pages.admin.user;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;
import org.wicketstuff.annotation.strategy.MountHybrid;

import com.gr.staffpm.StaffPMWebPage;
import com.gr.staffpm.datatypes.Group;
import com.gr.staffpm.datatypes.User;
import com.gr.staffpm.security.service.SortableUserDataProvider;
import com.gr.staffpm.security.service.UserService;
import com.gr.staffpm.widget.navigator.StaffPMNavigator;

@MountHybrid
@MountPath(path = "secure/admin/UserManagement")
public class AdminUsersPage extends StaffPMWebPage {

    @SpringBean(name = "userService")
    private UserService userService;

    public AdminUsersPage() {
        this(null);
    }

    public AdminUsersPage(String feedbackMsg) {

        SortableUserDataProvider dp = new SortableUserDataProvider(userService);

        add(new FeedbackPanel("feedback"));

        add(new AjaxLink<Void>("addUserLink") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target) {
                launchAddUserDialog(target);
            }

        });

        final DataView<User> dataView = new DataView<User>("users", dp) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(Item<User> item) {
                final User user = item.getModelObject();
                item.add(new Label("username", user.getUsername()));
                item.add(new Label("email", user.getEmailAddress()));
                item.add(new Label("fullname", user.getFullName()));
                String groups = "";
                for (Group group : user.getGroups()) {
                    if (groups.isEmpty())
                        groups += group.getName();
                    else
                        groups += " | " + group.getName();
                }
                item.add(new Label("groups", groups));
                BookmarkablePageLink<Void> operations = new BookmarkablePageLink<Void>("operations", EditUser.class, new PageParameters("id=" + user.getUserId()));

                //TODO Check here if user has permissions
                operations.add(new Label("edit", "Edit"));

                item.add(operations);
            }
        };
        dataView.setItemsPerPage(50);

        add(new OrderByBorder("orderByUserName", User.USERNAME, dp));
        add(new OrderByBorder("orderByEmail", User.EMAIL, dp));
        add(new OrderByBorder("orderByFullName", User.FULL_NAME, dp));

        add(dataView);
        add(new StaffPMNavigator("navigator", dataView));

        if (feedbackMsg != null)
            warn(feedbackMsg);

    }

    protected void launchAddUserDialog(AjaxRequestTarget target) {
        setResponsePage(AddUserPage.class);
    }

    @Override
    public String getTitle() {
        return "User Management - StaffPM";
    }

}
